Play
Show Transcript
Video info
00:00
Play
Seek 10 seconds backwards
Seek 10 seconds forward
00:00 / 11:19
Mute
Click to volume controlUse the arrows to control the volume
Enable Captions
Settings
Turn on Picture in picture
Show Full screen
More transcript options
Hide Transcript
Skip transcript
00:01
Collections are a very useful Python
00:03
feature that allow us to work with multiple
00:05
pieces of data in a single variable.
00:10
We've been doing these quite a bit
00:12
throughout the class, but I'm going to
00:13
do just a quick overview to make sure
00:15
we're all on the same page, and then
00:16
we'll talk about a few new features.
00:19
First off, what is a collection?
00:21
A collection is just a group of things.
00:23
Things are any value you
00:25
can store in a variable.
00:27
So a collection is a data type that
00:30
can contain more than one value.
00:33
If I need to keep track of more than one
00:35
number or string, I can create a collection
00:38
to contain all the numbers or strings
00:39
and store that in a single variable.
00:43
Python supports three
00:44
basic types of collections.
00:46
The first type is lists.
00:49
Lists can contain any kind of data.
00:51
You can even mix and match different kinds.
00:53
Arrays can only contain numbers,
00:56
and all the data. numbers
00:56
need to be of the same type.
00:58
So if I put an integer in,
01:00
all of the items have to be integers.
01:03
Finally, we have dictionaries.
01:05
Dictionaries contain a key value pair
01:07
of data. The key must be a string
01:09
and the value can be any type of data.
01:12
Let's take a look at lists. Now, you've
01:15
worked with lists quite a bit in the
01:16
course already, but just as a reminder,
01:18
to create a list, we just use square
01:20
brackets. We put each item that we want to be
01:22
contained in the list in square brackets
01:24
separated by commas. So here's a couple
01:26
examples and we've seen those quite a bit.
01:29
The concept that I would like to add
01:31
today is an item called a compound list.
01:34
A compound list is a list
01:36
that contains other lists.
01:38
Or we can create other kinds of
01:41
collections within collections. They
01:43
don't always have to just be lists.
01:45
But in this example, we have a list
01:46
that contains lists among other things.
01:49
Let's take a look at this Python code.
01:51
I have a variable called C-list. and
01:54
we're going to set that equal to a list
01:56
indicated by these outer square brackets.
02:00
The first item in the list is going
02:02
to be the number one, the second's
02:04
going to be the number two, the
02:05
third's going to be the number three,
02:07
and the fourth item in
02:08
the list, as you can see,
02:09
is actually another list.
02:12
That list has three
02:13
strings in it, A, B, and C.
02:16
So this list becomes a compound list
02:18
because the list has a list as one or
02:23
more of the end. elements in the list.
02:25
Here's the list that was in the slides.
02:29
So you can see we have a list that has the
02:31
numbers 1, 2, and 3, and a list. And the
02:34
list contains A, B, and C. Let's see what
02:37
that looks like from a print statement.
02:41
So we'll just print out the list.
02:43
The results of the list, as you can see,
02:45
look pretty much identical to our definition
02:47
of the list. So I think something
02:49
worked right. But let's explore that just
02:51
a little bit. If you remember to access
02:53
a given element in the list, we need to
02:56
know its number or its order in the list.
02:59
The first item in the list uses an
03:02
index value of zero. And so you kind of
03:04
can remember that. The first one is
03:06
zero. So our numbers are always one off.
03:09
So if I would to print the list with
03:12
index zero, that's going to give
03:14
me the very first item in the list,
03:16
which happens to be the number one.
03:19
If I want to get the fourth item in
03:21
the list, which is a list itself,
03:25
I'm going to put the index 3.
03:27
So I'll run that. You'll see that
03:29
I got the list, A, B, and C.
03:31
Let's show you another thing we could
03:33
do. If I want to, I can take a variable,
03:36
and I'll just create my variable
03:38
called V, just for fun. And I'm
03:40
going to say it equals whatever is
03:43
in the first position of the list.
03:45
Now, I'm going to print whatever is in
03:48
my variable V in my print statement.
03:52
So the thing that's in index 0 or
03:55
the first position in the list is 1.
03:57
If I were to change that to the
04:00
fourth position in the list with
04:01
index 3 and run it, you'll see that
04:05
my variable V now contains a list.
04:09
So in the print statement, I could choose
04:11
which item I want out of that list to print.
04:14
So I've got my variable that
04:16
contains this list, so V,
04:19
and I'm going to grab the second
04:21
item in this list, which is
04:23
going to be B. So I'll hit run,
04:25
you can see B.
04:26
Now let's talk about dictionaries.
04:29
A dictionary is just a type of a
04:31
collection where each item in the collection
04:33
is associated with a unique key.
04:35
You can retrieve the value in the
04:37
collection by using the values key.
04:40
A couple of things you need to keep in
04:41
mind, or a couple rules about dictionaries.
04:44
keys must be strings keys are case sensitive
04:48
keys must be unique and values can be
04:51
any type including other collections so a
04:54
dictionary could contain a list or a
04:56
dictionary could contain other dictionaries
04:58
let's take a look at how we would create a
05:00
dictionary it's pretty simple the main
05:03
thing you have to remember is that to add a
05:05
value in a dictionary you need to come up
05:08
with a unique key name that's going to be
05:10
associated with that value and you'll need
05:13
to know that key name each time you want
05:15
to access that value in the dictionary.
05:17
We also use braces rather than square
05:21
brackets when we create the dictionary.
05:23
So let's take a look at this Python code.
05:26
We have a variable that we call,
05:28
well, first of all, let's talk
05:29
about what we want to do here.
05:30
So the idea that I want to keep track of is
05:33
I want to keep track of books of scripture,
05:36
and I want to know how many chapters are
05:37
in the book and the name of the book.
05:39
So I'll create a variable that's
05:41
going to contain a dictionary.
05:42
I want to store the name of the book, so
05:44
a key that might be associated with the
05:47
name of the book might be the word name.
05:50
I want to know how many chapters are in
05:51
the book, so maybe a key to represent
05:53
that piece of data might be chapters.
05:57
Here we have a variable called book. We're
05:59
going to create a dictionary with a brace,
06:02
so then we'll use a key right here,
06:06
and then the value we want to
06:10
be associated with that key.
06:12
So first Nephi is going to be
06:13
the value associated with name.
06:15
And then the value associated with the
06:17
key chapters is going to be the number 20.
06:20
So let's create that book dictionary.
06:22
So I'll create a variable named book.
06:24
I'll use the braces to tell Python
06:26
we're creating a dictionary.
06:28
And then I need the key that I want
06:30
to associate with the first value
06:33
I want to add into my dictionary.
06:36
We're going to call this one name, and it's
06:38
going to represent the name of the book.
06:41
So the name of this book
06:42
is going to be first Nephi.
06:46
Then I'll use a comma,
06:48
and I can put the next key that I want.
06:51
The next T is going to be chapters,
06:53
and the value is going to be the number 20.
06:56
There are 20 chapters in First Nephi.
07:00
Let's see if that works just by
07:01
using the Python print statement.
07:03
We will print what's in the variable book.
07:08
So when I run this script,
07:11
you'll see that it outputs something that
07:12
looks suspiciously like the definition
07:14
of creating our dictionary on the first
07:18
line. So it seems like it's working okay.
07:21
Now that we have our dictionary created,
07:25
we need to learn how to access
07:26
an item within a dictionary.
07:28
To access an item within a dictionary,
07:31
what we're going to do is we're going
07:32
to use the key. So we need to know the
07:34
key that is associated with the value that
07:37
we're going to be. I want to return.
07:39
So if I want to return the value
07:42
that represents the name of the book
07:44
or is associated with a key name,
07:46
I will use the key like I would
07:49
use the index number in a list.
07:51
But in a dictionary, I use that
07:52
key. So it's got to be a string.
07:54
And it's the value that I used for the
07:57
key when I created the dictionary or
07:59
created that value in the dictionary.
08:02
So in my print statement.
08:04
If I wanted to print just the name,
08:07
I would use the key inside of our square
08:10
brackets for the variable book. Let me run
08:12
that. You'll see it printed first Nephi.
08:15
You may notice I may I spelled Nephi wrong.
08:18
We'll have to fix that in just a minute.
08:20
If I wanted to get the
08:22
number of chapters out of it,
08:24
I could access it using the key chapters.
08:27
So this is really useful. I might have
08:29
a thousand items in a dictionary and
08:31
as long as I know the key to get it out,
08:34
I can retrieve any individual value out of
08:36
the key. All right. Well, what happens
08:38
if you want to change the value of an item
08:40
in your dictionary? Here again, that's
08:42
super easy. All we need to do is know the key
08:45
that's associated with the value we want
08:47
to change. So the key in my case is name,
08:50
and I want to fix my spelling of Nephi.
08:53
So let me go back to my code.
08:55
And right after my print statement, I'm going
08:58
to fix the name. So we're going to print
09:00
this out a couple times. But I'll say book,
09:03
with the key of name,
09:06
whoa,
09:07
let's spell name right.
09:09
I'll set that equal to First Nephi.
09:13
And maybe I've spelled Nephi
09:15
a little bit better this time.
09:17
So now I'm going to print the book
09:19
so we can see what results that had.
09:21
Run my Python code.
09:23
You'll see that the output from
09:24
my code shows the name value
09:27
being corrected to First Nephi.
09:29
If I want to add a new value to the
09:31
dictionary, all I need to do is come up with
09:34
a key that has not been used and use it
09:36
to identify the value I want to store.
09:39
So in this case, I want to know
09:40
which volume of our standard works
09:43
this book happens to belong to.
09:45
So I could create a key called
09:47
something like standard work,
09:49
and I'll set it equal
09:49
to the Book of ******.
09:52
So let's take our book,
09:55
our new PSD-D work,
09:59
and we're going to set that to
10:01
the value. of book of ******.
10:06
Now let's go ahead and print book again.
10:10
When I run my code,
10:12
you'll see that the last print in
10:14
there now has a key of standard work
10:17
with the value of Book of ******.
10:19
The last thing that I want to
10:21
show you is how you could remove
10:22
an item out of a dictionary.
10:25
This is super easy. We just use the delete
10:27
keyword and the key that's associated
10:30
with the value that I want to remove.
10:32
So if I wanted to get rid of that
10:34
standard work that I just added,
10:36
the code would look like this,
10:37
delete book, and then the key
10:38
standard work. Let's try that.
10:41
So I just say delete
10:42
from my variable book,
10:45
and we'll put the string
10:46
standard work in there.
10:48
Let's print that
10:50
so we can see if it worked.
10:53
All right, let's look down in our code.
10:55
So this print line is after we added
10:57
it, then we deleted it, and this is
10:59
the final print line where it's gone.
11:01
So we've learned how to create
11:03
dictionaries, we've learned how to add
11:05
items to dictionaries, we've learned how
11:06
to remove items from dictionaries,
11:08
and we've learned how to update items
11:10
in a dictionary. And those are
11:11
about all the things that you're really
11:12
going to do with the dictionary.
11:14
Dictionaries have a lot of other
11:16
features that we're not going to
11:17
cover in this particular video.
Resume AutoScroll
Copy debug info